/** 

`/n* @author: 谢明霁
* @date：年7月9日 上午::
* @version 1.0 
*/
package controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.bind.v2.runtime.Name;

import bean.User;
import service.UserService;

@Controller
public class PrivateManagerController {
	private UserService service = new UserService();

	@RequestMapping("/privateManager")
	public String toPage() {
		return "userInfo";
	}

	@RequestMapping(value = "/fileRecieve", method = RequestMethod.POST)
	public ResponseEntity<JSONObject> recieveFile(HttpServletRequest request, HttpServletResponse response) {
		// 得到上传文件的保存目录，将上传的文件存放于WEB-INF目录下，不允许外界直接访问，保证上传文件的安全
		String savePath = request.getServletContext().getInitParameter("uploadFilePath");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		final MultipartFile photo = multipartRequest.getFile("files[]");
		System.out.println();
		File dir = new File(savePath);
		// 判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			System.out.println(savePath + "目录不存在，需要创建");
			// 创建目录
			dir.mkdir();
		}
		final String fileName = photo.getOriginalFilename();
		try {
			File file = new File(savePath, fileName);
			photo.transferTo(file);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject json = new JSONObject();
		json.put("result", true);
		json.put("url", "upload/" + fileName);
		final User user = (User) request.getSession().getAttribute("user");
		user.setImg("upload/" + fileName);
		service.modifyUserInfo(user);
		request.getSession().removeAttribute("user");
		request.getSession().setAttribute("user", user);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json, responseHeaders,
				HttpStatus.OK);
		return responseResult;
	}

	@RequestMapping(value = "/privateManagerModify", method = RequestMethod.POST)
	public ResponseEntity<JSONObject> modifyUserInfo(HttpServletRequest request, HttpServletResponse response) {
		final Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String string = (String) parameterNames.nextElement();
			final String parameter = request.getParameter(string);
			System.out.println(string + " : " + parameter);
		}
		final String realName = request.getParameter("realName");
		final String sex = request.getParameter("sex");
		final String phoneNum = request.getParameter("phoneNum");
		final String birthday = request.getParameter("birthday");
		final User user = (User) request.getSession().getAttribute("user");
		user.setBirthday(birthday);
		service.modifyUserInfo(user);
		request.getSession().removeAttribute("user");
		request.getSession().setAttribute("user", user);
		JSONObject json = new JSONObject();
		json.put("result", true);
		json.put("birthday", birthday);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json, responseHeaders,
				HttpStatus.OK);
		return responseResult;
	}
}
